<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Trend;
use app\util\Util;
use app\util\File;
use think\Log;

/*
 * 企业动态
 */
class TrendController extends CommonController
{
    /*
     * 文章列表
     */
    public function index()
    {
        $list = Trend::field('id, title, source, add_time')
            ->order('add_time desc')
            ->paginate(15);

        return view('index', [
            'list' => $list
        ]);
    }

    /*
     * 添加文章
     */
    public function add()
    {
        return view('add');
    }

    /*
     * 写入文章
     */
    public function postInsert()
    {
        $all = input('post.');

        $title = $all['title'];
        $source = $all['source'];
        $img = $all['img'];
        $content = $all['content'];

        $exist = Trend::where('title', '=', $title)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '文章已存在', null);
        }

        $data = [
            'title' => $title,
            'source' => $source,
            'img' => $img,
            'content' => $content,
            'add_time' => time(),
        ];

        $result = Trend::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
     * 编辑文章
     */
    public function edit()
    {
        $id = input('param.id');

        $row = Trend::where('id', '=', $id)->find();

        return view('edit', [
            'id' => $id,
            'row' => $row,
        ]);
    }

    /*
     * 保存文章
     */
    public function postSave()
    {
        $all = input('post.');

        $id = $all['id'];
        $title = $all['title'];
        $source = $all['source'];
        $img = $all['img'];
        $content = $all['content'];

        $exist = Trend::where('title', '=', $title)
            ->where('id', '<>', $id)
            ->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '文章已存在', null);
        }

        $data = [
            'title' => $title,
            'source' => $source,
            'content' => $content,
        ];

        Log::info(10000000);
        Log::info($img);

        $row = null;
        if ($img != '') {
            $row = Trend::where('id', '=', $id)->field('img')->find();

            $data['img'] = $img;
        }

        $result = Trend::where('id', '=', $id)->update($data);
        if ($result) {
            if ($img != '') {
                $file = new File();
                $file->deleteImage($row['img']);
            }

            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
     * 删除文章
     */
    public function del()
    {
        $id = input('param.id');

        $row = Trend::where('id', '=', $id)->field('img')->find();

        $result = Trend::where('id', '=', $id)->delete();
        if ($result) {
            $file = new File();
            $file->deleteImage($row['img']);

            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }

}